package com.lalilu.homework8.utils;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import androidx.annotation.Nullable;

public class DBHelper extends SQLiteOpenHelper {
    public static final String TAG = DBHelper.class.getName();
    private static volatile DBHelper instance = null;

    private DBHelper(@Nullable Context context) {
        super(context, "sqlite.db", null, 1);
    }

    public static DBHelper getInstance(@Nullable Context context) {
        if (instance == null) {
            synchronized (DBHelper.class) {
                if (instance == null) {
                    instance = new DBHelper(context);
                }
            }
        }
        if (instance == null) throw new RuntimeException("no context input.");
        return instance;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE users (id INTEGER PRIMARY KEY AUTOINCREMENT," +
                "username TEXT, age INTEGER)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w(TAG, "Database onUpgrade: from " + oldVersion + " to " + newVersion);
    }
}
